﻿namespace HutongGames.PlayMaker
{
    using System;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    [Serializable]
    public class FsmState : INameable, IBlock
    {
        [CompilerGenerated]
        private float RealStartTime__BackingField;
        [CompilerGenerated]
        private float StateTime__BackingField;
        [SerializeField]
        private HutongGames.PlayMaker.ActionData actionData = new HutongGames.PlayMaker.ActionData();
        [NonSerialized]
        private FsmStateAction[] actions;
        private bool active;
        [SerializeField]
        private byte colorIndex;
        [SerializeField]
        private string description;
        private bool finished;
        [NonSerialized]
        private HutongGames.PlayMaker.Fsm fsm;
        [SerializeField]
        private bool hideUnused;
        [SerializeField]
        private bool isBreakpoint;
        [SerializeField]
        private string name;
        [SerializeField]
        private Rect position;
        [SerializeField]
        private FsmTransition[] transitions = new FsmTransition[0];

        public FsmState(HutongGames.PlayMaker.Fsm fsm)
        {
            this.fsm = fsm;
        }

        private void CheckFinished()
        {
            if (this.active)
            {
                foreach (FsmStateAction action in this.Actions)
                {
                    if (!action.Finished)
                    {
                        return;
                    }
                }
                this.finished = true;
                this.fsm.Event(FsmEvent.Finished);
            }
        }

        public void CopyActionData(FsmState state)
        {
            this.actionData = state.actionData.Copy();
        }

        public static string GetFullStateLabel(FsmState state)
        {
            if (state == null)
            {
                return "None (State)";
            }
            return (HutongGames.PlayMaker.Fsm.GetFullFsmLabel(state.Fsm) + " : " + state.Name);
        }

        public void LoadActions()
        {
            this.actions = this.actionData.LoadActions(this);
        }

        public void OnEnter()
        {
            this.active = true;
            this.finished = false;
            this.RealStartTime = FsmTime.RealtimeSinceStartup;
            this.StateTime = 0f;
            foreach (FsmStateAction action in this.Actions)
            {
                if (!action.Enabled)
                {
                    action.Finished = true;
                }
                else
                {
                    action.Finished = false;
                    HutongGames.PlayMaker.Fsm.ExecutingAction = action;
                    action.Init(this);
                    action.OnEnter();
                    HutongGames.PlayMaker.Fsm.ExecutingAction = null;
                    if (this != this.Fsm.ActiveState)
                    {
                        return;
                    }
                }
            }
            this.CheckFinished();
        }

        public void OnExit()
        {
            this.active = false;
            this.finished = false;
            foreach (FsmStateAction action in this.Actions)
            {
                if (action.Enabled)
                {
                    HutongGames.PlayMaker.Fsm.ExecutingAction = action;
                    action.Init(this);
                    action.OnExit();
                }
            }
        }

        public void OnFixedUpdate()
        {
            if (!this.finished)
            {
                foreach (FsmStateAction action in this.Actions)
                {
                    if (action.Active)
                    {
                        HutongGames.PlayMaker.Fsm.ExecutingAction = action;
                        action.Init(this);
                        action.OnFixedUpdate();
                        HutongGames.PlayMaker.Fsm.ExecutingAction = null;
                        if (this != this.Fsm.ActiveState)
                        {
                            return;
                        }
                    }
                }
                this.CheckFinished();
            }
        }

        public void OnLateUpdate()
        {
            if (!this.finished)
            {
                foreach (FsmStateAction action in this.Actions)
                {
                    if (action.Active)
                    {
                        HutongGames.PlayMaker.Fsm.ExecutingAction = action;
                        action.Init(this);
                        action.OnLateUpdate();
                        HutongGames.PlayMaker.Fsm.ExecutingAction = null;
                        if (this != this.Fsm.ActiveState)
                        {
                            return;
                        }
                    }
                }
                this.CheckFinished();
            }
        }

        public void OnUpdate()
        {
            if (!this.finished)
            {
                this.StateTime += Time.deltaTime;
                foreach (FsmStateAction action in this.Actions)
                {
                    if (action.Active)
                    {
                        HutongGames.PlayMaker.Fsm.ExecutingAction = action;
                        action.Init(this);
                        action.OnUpdate();
                        HutongGames.PlayMaker.Fsm.ExecutingAction = null;
                        if (this != this.Fsm.ActiveState)
                        {
                            return;
                        }
                    }
                }
                this.CheckFinished();
            }
        }

        public void SaveActions()
        {
            if (this.actions != null)
            {
                this.actionData.SaveActions(this.actions);
            }
        }

        public HutongGames.PlayMaker.ActionData ActionData
        {
            get
            {
                return this.actionData;
            }
        }

        public FsmStateAction[] Actions
        {
            get
            {
                if (this.fsm == null)
                {
                    Debug.LogError("get_actions: Fsm not initialized: " + this.name);
                }
                return (this.actions ?? (this.actions = this.actionData.LoadActions(this)));
            }
            set
            {
                this.actions = value;
            }
        }

        public bool Active
        {
            get
            {
                return this.active;
            }
        }

        public int ColorIndex
        {
            get
            {
                return this.colorIndex;
            }
            set
            {
                this.colorIndex = (byte) value;
            }
        }

        public string Description
        {
            get
            {
                return (this.description ?? (this.description = ""));
            }
            set
            {
                this.description = value;
            }
        }

        public HutongGames.PlayMaker.Fsm Fsm
        {
            get
            {
                if (this.fsm == null)
                {
                    Debug.LogError("get_fsm: Fsm not initialized: " + this.name);
                }
                return this.fsm;
            }
            set
            {
                if (value == null)
                {
                    Debug.LogWarning("set_fsm: value == null: " + this.name);
                }
                this.fsm = value;
            }
        }

        public bool HideUnused
        {
            get
            {
                return this.hideUnused;
            }
            set
            {
                this.hideUnused = value;
            }
        }

        public bool IsBreakpoint
        {
            get
            {
                return this.isBreakpoint;
            }
            set
            {
                this.isBreakpoint = value;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        public Rect Position
        {
            get
            {
                return this.position;
            }
            set
            {
                this.position = value;
            }
        }

        public float RealStartTime
        {
            [CompilerGenerated]
            get
            {
                return this.RealStartTime__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                this.RealStartTime__BackingField = value;
            }
        }

        public float StateTime
        {
            [CompilerGenerated]
            get
            {
                return this.StateTime__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                this.StateTime__BackingField = value;
            }
        }

        public FsmTransition[] Transitions
        {
            get
            {
                return this.transitions;
            }
            set
            {
                this.transitions = value;
            }
        }
    }

    public interface IBlock
    {
        Rect Position { get; set; }
    }
}

